home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 with MFC / Programming Windows 95 with MFC (Microsoft Programming Series)(097-0001465)(1996).iso / CODE / Chap03 / GdiDemo4 / GdiDemo4.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-05  |  5.7 KB  |  231 lines

  1. //***********************************************************************
  2. //
  3. //  GdiDemo4.cpp
  4. //
  5. //***********************************************************************
  6.  
  7. #include <afxwin.h>
  8. #include "GdiDemo4.h"
  9.  
  10. #define VHEIGHT (m_cyChar * 50)
  11.  
  12. CMyApp myApp;
  13.  
  14. /////////////////////////////////////////////////////////////////////////
  15. // CMyApp member functions
  16.  
  17. BOOL CMyApp::InitInstance ()
  18. {
  19.     m_pMainWnd = new CMainWindow;
  20.     m_pMainWnd->ShowWindow (m_nCmdShow);
  21.     m_pMainWnd->UpdateWindow ();
  22.     return TRUE;
  23. }
  24.  
  25. /////////////////////////////////////////////////////////////////////////
  26. // CMainWindow message map and member functions
  27.  
  28. BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
  29.     ON_WM_CREATE ()
  30.     ON_WM_SETFOCUS ()
  31. END_MESSAGE_MAP ()
  32.  
  33. CMainWindow::CMainWindow ()
  34. {
  35.     m_pMyView = NULL;
  36.     Create (NULL, "GdiDemo4");
  37. }
  38.  
  39. int CMainWindow::OnCreate (LPCREATESTRUCT lpcs)
  40. {
  41.     if (CFrameWnd::OnCreate (lpcs) == -1)
  42.         return -1;
  43.  
  44.     m_pMyView = new CMyView;
  45.     m_pMyView->Create (NULL, NULL, WS_CHILD | WS_VISIBLE,
  46.         CRect (0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, NULL);
  47.     return 0;   
  48. }
  49.     
  50. void CMainWindow::OnSetFocus (CWnd* pWnd)
  51. {
  52.     if (m_pMyView != NULL)
  53.         m_pMyView->SetFocus ();
  54. }
  55.  
  56. /////////////////////////////////////////////////////////////////////////
  57. // CMyView message map and member functions
  58.  
  59. BEGIN_MESSAGE_MAP (CMyView, CScrollView)
  60.     ON_WM_CREATE ()
  61.     ON_WM_SIZE ()
  62.     ON_WM_KEYDOWN ()
  63. END_MESSAGE_MAP ()
  64.     
  65. int CMyView::OnCreate (LPCREATESTRUCT lpcs)
  66. {
  67.     if (CScrollView::OnCreate (lpcs) == -1)
  68.         return -1;
  69.     
  70.     TEXTMETRIC tm;
  71.     CClientDC dc (this);
  72.     dc.GetTextMetrics (&tm);
  73.     m_cxChar = tm.tmAveCharWidth;
  74.     m_cyChar = tm.tmHeight;
  75.     return 0;
  76. }
  77.  
  78. void CMyView::OnSize (UINT nType, int cx, int cy)
  79. {
  80.     SetScrollSizes (MM_TEXT, CSize (0, VHEIGHT), CSize (0, cy),
  81.         CSize (0, m_cyChar));
  82. }
  83.  
  84. void CMyView::OnDraw (CDC* pDC)
  85. {
  86.     CPaintDC dc (this);
  87.                 
  88.     ShowPenStyles (pDC, m_cxChar * 2, m_cyChar);   
  89.     ShowPenWidths (pDC, m_cxChar * 2, m_cyChar * 15);
  90.     ShowBrushStyles (pDC, m_cxChar * 2, m_cyChar * 27);
  91. }
  92.  
  93. void CMyView::OnKeyDown (UINT nChar, UINT nRepCnt, UINT nFlags)
  94. {
  95.     switch (nChar) {
  96.  
  97.     case VK_UP:
  98.         SendMessage (WM_VSCROLL, SB_LINEUP, 0);
  99.         break;
  100.  
  101.     case VK_PRIOR:
  102.         SendMessage (WM_VSCROLL, SB_PAGEUP, 0);
  103.         break;
  104.  
  105.     case VK_NEXT:
  106.         SendMessage (WM_VSCROLL, SB_PAGEDOWN, 0);
  107.         break;
  108.  
  109.     case VK_DOWN:
  110.         SendMessage (WM_VSCROLL, SB_LINEDOWN, 0);
  111.         break;
  112.     }
  113. }
  114.  
  115. void CMyView::ShowPenStyles (CDC* pDC, int x, int y)
  116. {
  117.     static struct STYLES styles[] = {
  118.         PS_SOLID,       "PS_SOLID",
  119.         PS_DASH,        "PS_DASH",
  120.         PS_DOT,         "PS_DOT",
  121.         PS_DASHDOT,     "PS_DASHDOT",
  122.         PS_DASHDOTDOT,  "PS_DASHDOTDOT",
  123.         PS_NULL,        "PS_NULL",
  124.         PS_INSIDEFRAME, "PS_INSIDEFRAME"
  125.     };
  126.  
  127.     pDC->SetTextColor (RGB (0, 0, 0));
  128.     pDC->TextOut (x, y, "Pen Styles");
  129.  
  130.     int dy = (m_cyChar * 3) / 2;
  131.     int x1 = x + (m_cxChar * 2);
  132.     int x2 = x + (m_cxChar * 22);
  133.     int x3 = x + (m_cxChar * 46);
  134.  
  135.     CPen* pOldPen;
  136.     pDC->SetTextColor (RGB (255, 0, 0));
  137.  
  138.     for (int i=0; i<7; i++) {
  139.         y += dy;
  140.         pDC->TextOut (x1, y, styles[i].szStyleName);
  141.  
  142.         CPen pen (styles[i].nStyle, 1, RGB (255, 0, 0));
  143.         pOldPen = pDC->SelectObject (&pen);
  144.  
  145.         pDC->MoveTo (x2, y + (m_cyChar / 2));
  146.         pDC->LineTo (x3, y + (m_cyChar / 2));
  147.  
  148.         pDC->SelectObject (pOldPen);
  149.     }
  150. }
  151.  
  152. void CMyView::ShowPenWidths (CDC* pDC, int x, int y)
  153. {
  154.     static int nPenWidths[] = { 2, 8, 16, 24 };
  155.  
  156.     pDC->SetTextColor (RGB (0, 0, 0));
  157.     pDC->TextOut (x, y, "Pen Widths");
  158.  
  159.     int dy = m_cyChar * 2;
  160.     int x1 = x + (m_cxChar * 2);
  161.     int x2 = x + (m_cxChar * 22);
  162.     int x3 = x + (m_cxChar * 46);
  163.  
  164.     CPen* pOldPen;
  165.     CString strDescription;
  166.     pDC->SetTextColor (RGB (0, 0, 255));
  167.  
  168.     for (int i=0; i<4; i++) {
  169.         y += dy;
  170.         strDescription.Format ("%d Pixels", nPenWidths[i]);
  171.         pDC->TextOut (x1, y, strDescription);
  172.  
  173.         CPen pen (PS_SOLID, nPenWidths[i], RGB (0, 0, 255));
  174.         pOldPen = pDC->SelectObject (&pen);
  175.  
  176.         pDC->MoveTo (x2, y + (m_cyChar / 2));
  177.         pDC->LineTo (x3, y + (m_cyChar / 2));
  178.  
  179.         pDC->SelectObject (pOldPen);
  180.     }
  181. }
  182.  
  183. void CMyView::ShowBrushStyles (CDC* pDC, int x, int y)
  184. {
  185.     static struct STYLES styles[] = {
  186.         HS_BDIAGONAL,   "HS_BDIAGONAL",
  187.         HS_CROSS,       "HS_CROSS",
  188.         HS_DIAGCROSS,   "HS_DIAGCROSS",
  189.         HS_FDIAGONAL,   "HS_FDIAGONAL",
  190.         HS_HORIZONTAL,  "HS_HORIZONTAL",
  191.         HS_VERTICAL,    "HS_VERTICAL"
  192.     };
  193.  
  194.     pDC->SetTextColor (RGB (0, 0, 0));
  195.     pDC->TextOut (x, y, "Brush Styles");
  196.  
  197.     int dy = m_cyChar * 3;
  198.     int x1 = x + (m_cxChar * 2);
  199.     int x2 = x + (m_cxChar * 22);
  200.     int x3 = x + (m_cxChar * 46);
  201.  
  202.     CBrush* pOldBrush;
  203.  
  204.     for (int i=0; i<6; i++) {
  205.         y += dy;
  206.         pDC->TextOut (x1, y, styles[i].szStyleName);
  207.  
  208.         CRect rect (x2, y - m_cyChar, x3, y + m_cyChar);
  209.         CBrush brush (styles[i].nStyle, RGB (0, 255, 0));
  210.  
  211.         CPoint point (rect.left, rect.top);
  212.         pDC->LPtoDP (&point);
  213.         point.x %= 8;
  214.         point.y %= 8;
  215.         pDC->SetBrushOrg (point);
  216.  
  217.         pOldBrush = pDC->SelectObject (&brush);
  218.         pDC->Rectangle (rect);
  219.  
  220.         pDC->SelectObject (pOldBrush);
  221.     }
  222.  
  223.     y += dy;
  224.     pDC->TextOut (x1, y, "Solid");
  225.  
  226.     CBrush brush (RGB (0, 255, 0));
  227.     pOldBrush = pDC->SelectObject (&brush);
  228.     pDC->Rectangle (x2, y - m_cyChar, x3, y + m_cyChar);
  229.     pDC->SelectObject (pOldBrush);
  230. }
  231.